package com.thero.core.command;

import com.thero.core.net.MessageHeader;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by wusk on 2017/11/28.
 *  网络命令字对应处理器
 */
public class Command {

    private static Set<Short> locations = new HashSet<>(24);

    static {
        locations.add((short) 11);
        locations.add((short) 22);
        locations.add((short) 33);

        locations.add((short) 55);
        locations.add((short) 66);
        locations.add((short) 77);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);

        locations.add((short) 11);
        locations.add((short) 11);
        locations.add((short) 11);
    }

    private MessageHeader header;

    public Command(MessageHeader h){
        header = h;
    }

    public MessageHeader getHeader(){
        return header;
    }

    /**
     * 从字节中读取int
     *  一个byte数据左移24位变成0x??000000，再右移8位变成0x00??0000
     * @param b0 0
     * @param b1 1
     * @param b2 2
     * @param b3 3
     * @return int
     */
    public int readInt(byte b0, byte b1, byte b2, byte b3) {
        int result = (b0 & 0xff) | ((b1 << 8) & 0xff00) | ((b2 << 24) >>> 8) | (b3 << 24);
        return result;
    }

    /**
     * 从字节中读取short
     * @param b0 0
     * @param b1 1
     * @return short
     */
    public short readShort(byte b0, byte b1){
        short s = 0;
        short s0 = (short) (b0 & 0xff);// 最低位
        short s1 = (short) (b1 & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

    public static boolean checkLocation(short location){
        return locations.contains(location);
    }
}
